package com.example.fish.service.impl;

import com.example.fish.mapper.UserInfoMapper;
import com.example.fish.mapper.UserMapper;
import com.example.fish.pojo.User;
import com.example.fish.pojo.UserExample;
import com.example.fish.pojo.UserInfo;
import com.example.fish.service.UserService;
import com.example.fish.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Locale;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;


    /***
     * 用户登录的操作
     * @param username 用户名
     * @param password 密码
     * @return 根据用户名查询返回的User对象
     */
    @Override
    public User login(String username,String password) {
//        验证用户名是否存在
        UserExample example= new UserExample();
        example.createCriteria().andUsernameEqualTo(username);
        Integer i = userMapper.countByExample(example);
        if (i == 0) {
            throw new UserNotExistsException("用户不存在！");
        }
//        验证密码
        User user = userMapper.findByUserName(username);
        String md5Password = getMd5Password(password, user.getSalt());
        if (!user.getPassword().equals(md5Password) && user.getIsdelete()!=1) {
            throw new UserNotExistsException("密码错误！");
        }
        User user1 = new User(user.getUserid(),user.getUsername(),null,user.getRole(),null,null);
        return user1;
    }


    /***
     * 用户注册
     * @param user
     * @return
     */
    @Override
    public Integer insertUser(User user) {
        User byUserName = userMapper.findByUserName(user.getUsername());
//        用户存在
        if (byUserName != null) {
            throw new UserExistsException("用户名已存在");
        }
//        用户不存在则
//        密码加密
        //        随机生成一个盐值
        String salt = UUID.randomUUID().toString().toUpperCase(Locale.ROOT);
        String password = user.getPassword();
        String newPassword = getMd5Password(password,salt);
        user.setPassword(newPassword);
        user.setSalt(salt);
        user.setRole(0);
        int result = userMapper.insert(user);
        //插入失败
        if (result != 1) {
            throw new UserInsertException("插入失败！");
        }
        User regUser = userMapper.findByUserName(user.getUsername());
//        初始化用户详细信息
        UserInfo userInfo = new UserInfo();
        userInfo.setUserid(regUser.getUserid());
        userInfo.setName("钓鱼用户");
        userInfo.setSex(1);
        userInfo.setEmail("123@163.com");
        userInfo.setAvator("admin.jpg");
        userInfo.setFishage(0);
        userInfo.setRegdate("2000-01-01");
        userInfo.setAddress("北京");
        userInfo.setBackimg("admin.jpg");
        int insert = userInfoMapper.insert(userInfo);
        if (insert != 1){
            throw new UserInsertException("插入失败！");
        }
        return result;
    }

    /***
     * 修改密码的操作
     * @param oldPassword  旧密码
     * @param newPassword 新密码
     * @param user user对象
     * @return 1 修改成功
     */
    @Override
    public Integer updatePassword(String oldPassword,String newPassword, User user) {
        Integer userid = user.getUserid();
        User user1 = userMapper.selectByPrimaryKey(userid);
//        核对密码 -- 将旧密码oldPassword通过加密后比对
        String md5Password = getMd5Password(oldPassword,user1.getSalt());
        if (!user1.getPassword().equals(md5Password)){
//            核对失败
            throw new UserUpdatePasswordException("密码不一致!");
        }
//        核对成功, 修改密码
        String salt = UUID.randomUUID().toString().toUpperCase(Locale.ROOT);
        String newMD5Password = getMd5Password(newPassword, salt);
        user1.setSalt(salt);
        user1.setPassword(newMD5Password);
//        存入数据库
        int i = userMapper.updateByPrimaryKey(user1);
        if (i != 1){
            throw  new UserSavePasswordException("密码保存失败！");
        }
        return i;
    }

    @Override
    public User adminLogin(String username, String password) {
        User byUserName = userMapper.findByUserName(username);
        String salt = "1";
        if (byUserName.getPassword().equals(password) && byUserName.getSalt().equals(salt)){
            return byUserName;
        }else {
            return null;
        }
    }

    //    md5算法的加密
    private String getMd5Password(String password,String salt){
//        加密算法的方法调用(进行三次加密)
        for (int i = 0; i < 2; i++) {
            password = DigestUtils.md5DigestAsHex((salt + password + salt).getBytes()).toUpperCase();
        }
        return password;
    }

}
